package com.example.testproject.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testproject.R;
import com.example.testproject.bean.WanAndroidBean2;
import com.example.testproject.network.NetWorkApi;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @ClassName: RetrofitActivity
 * @Author: android 超级兵
 * @CreateDate: 12/23/21$ 3:00 PM$
 * TODO Retrofit源码分析
 */
public class RetrofitActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
        TextView tvContent = findViewById(R.id.tvContent);


        findViewById(R.id.bt).setOnClickListener(v -> {
            tvContent.setTextColor(Color.parseColor("#00FFFF"));
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(NetWorkApi.url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            NetWorkApi netWorkApi = retrofit.create(NetWorkApi.class);
            netWorkApi.getHomeData("0").enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(@NotNull Call<ResponseBody> call, @NotNull Response<ResponseBody> response) {
                    try {
                        tvContent.setText("网络请求1请求数据:\n" + response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(@NotNull Call<ResponseBody> call, @NotNull Throwable t) {
                    tvContent.setText(t.getMessage());
                }
            });
        });

        findViewById(R.id.bt2).setOnClickListener(v -> {
            tvContent.setTextColor(Color.parseColor("#FF00FF"));
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(NetWorkApi.url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            NetWorkApi netWorkApi = retrofit.create(NetWorkApi.class);
            netWorkApi.getHomeData2("0").enqueue(new Callback<WanAndroidBean2>() {
                @Override
                public void onResponse(@NotNull Call<WanAndroidBean2> call, @NotNull Response<WanAndroidBean2> response) {
                    tvContent.setText("网络请求2请求数据:\n" + response.body().toString());
                }

                @Override
                public void onFailure(@NotNull Call<WanAndroidBean2> call, @NotNull Throwable t) {
                    tvContent.setText(t.getMessage());
                }
            });
        });


        findViewById(R.id.bt3).setOnClickListener(v -> {
            tvContent.setTextColor(Color.parseColor("#000000"));
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(NetWorkApi.url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            NetWorkApi netWorkApi = retrofit.create(NetWorkApi.class);
            netWorkApi.getHomeData3("0")
                    .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                    .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口

                    .subscribe(new Subscriber<WanAndroidBean2>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            tvContent.setText(e.getMessage());
                        }

                        @Override
                        public void onNext(WanAndroidBean2 wanAndroidBean2) {
                            tvContent.setText(wanAndroidBean2.toString());
                        }
                    });
        });
    }
}
